package com.lock.subscribe.util;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public final static Integer fiveMinDuration = 300;
	public final static Integer threeDayDuration = 4115;
	public final static Integer ninetythreeDayDuration = 126172;
	public final static Integer oneyearDuration = 501943;
	public final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//10
	public final static SimpleDateFormat dateFormatYYYYMM = new SimpleDateFormat("yyyy-MM");//7
	public final static SimpleDateFormat dateFormat8 = new SimpleDateFormat("yyyyMMdd");//8
	public final static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//19
	public final static SimpleDateFormat dateFormatYYYYMMDDHH = new SimpleDateFormat("yyyy-MM-dd HH");//13
	public final static SimpleDateFormat dateFormatYYYYMMDDHHmm = new SimpleDateFormat("yyyy-MM-dd HH:mm");//16
	public final static SimpleDateFormat dateFormatYYYYMMDDHHmmSSS = new SimpleDateFormat("yyMMddHHmmss");//23
	
	public final static SimpleDateFormat[] dateFormats = new SimpleDateFormat[]{null,
			null,null,null,null,null,null,dateFormatYYYYMM,dateFormat8,null,dateFormat,null,null,dateFormatYYYYMMDDHH
			,null,null,dateFormatYYYYMMDDHHmm, null, null, dateTimeFormat, null, null, null, dateFormatYYYYMMDDHHmmSSS
	};

	public final static String ONE_WEEK_BEFORE = oneWeekBefore();
	public final static String ONE_MONTH_BEFORE = oneMonthBefore();
	
	public static <T> Object to(String dateStr, Class<T> toDateClass){
		if(toDateClass==Date.class){
			return toDate(dateStr);
		}else if(toDateClass==Timestamp.class){
			return toTimestamp(dateStr);
		}else if(toDateClass==java.sql.Date.class){
			return toSqlDate(dateStr);
		}
		return null;
	}
	
	public static Date toDate(String dateStr){
		if(dateStr==null){
			return null;
		}
		return toDate(dateStr, dateFormats[dateStr.length()]);
	}
	
	public static Date toDate(String dateStr, String formatStr){
		return toDate(dateStr, new SimpleDateFormat(formatStr));
	}
	
	public static Date toDate(String dateStr, SimpleDateFormat format){
		if(dateStr==null || "".equals(dateStr)){
			return null;
		}
		if(format==null){
			return null;
		}
		try {
			return format.parse(dateStr);
		} catch (ParseException e) {
			throw new RuntimeException("解析"+dateStr+"成日期异常");
		}
	}
	
	public static Timestamp toTimestamp(String dateStr){
		if(dateStr==null){
			return null;
		}
		return toTimestamp(dateStr, dateFormats[dateStr.length()]);
	}
	
	public static Timestamp toTimestamp(String dateStr, String formatStr){
		return toTimestamp(dateStr, formatStr);
	}
	
	public static Timestamp toTimestamp(String dateStr, SimpleDateFormat format){
		Date date = toDate(dateStr, format);
		if(date==null){
			return null;
		}
		return new Timestamp(date.getTime());
	}
	
	public static java.sql.Date toSqlDate(String dateStr){
		if(dateStr==null){
			return null;
		}
		return toSqlDate(dateStr, dateFormats[dateStr.length()]);
	}
	
	public static java.sql.Date toSqlDate(String dateStr, String formatStr){
		return toSqlDate(dateStr, formatStr);
	}
	
	public static java.sql.Date toSqlDate(String dateStr, SimpleDateFormat format){
		Date date = toDate(dateStr, format);
		if(date==null){
			return null;
		}
		return new java.sql.Date(date.getTime());
	}
	
	public static String nowDate(){
		return dateFormat.format(System.currentTimeMillis());
	}
	public static String now(String pattern){
		return new SimpleDateFormat(pattern).format(System.currentTimeMillis());
	}
	public static String now(SimpleDateFormat simpleDateFormat){
		return simpleDateFormat.format(System.currentTimeMillis());
	}
	public static String nowDateTime(){
		return dateTimeFormat.format(System.currentTimeMillis());
	}
	public static String longToStringTime(Long stamp){
		return dateTimeFormat.format(stamp);
	}
	public static String dateTime(Long stamp){
		stamp = stamp*1000 -60 * 60 * 8L ;
		return dateTimeFormat.format(stamp);
	}
	public static Long getNowDateTime(){
		Long time = System.currentTimeMillis()/1000L ; 
		return time ;
	}
	public static Long getNowTime(){
		Long time = System.currentTimeMillis()/1000L+ 60 * 60 * 8L ; 
		return time ;
	}
	public static Long getNowTimeSub(int day){
		Long time = System.currentTimeMillis()/1000L- day*24*60*60; ; 
		return time ;
	}
	public static Long getNowSub(int day){
		Long time = LocalDate.now()
			     .atStartOfDay(ZoneOffset.ofHours(8))
			     .toInstant()
			     .toEpochMilli()/1000- day*24*60*60 ; 
		return time ;
	}

	public static Long getNowTimeAdd(int day){
		Long time = System.currentTimeMillis()/1000L + day*24*60*60;
		return time ;
	}
	public static Long getTimeAdd(long time,int day){
		return time + day*24*60*60;
	}
	public static String getNowTimeStr(){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		return simpleDateFormat.format(date);
	}
	public static String getTimeStr(int y){
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);//设置起时间
		cal.add(Calendar.YEAR, y);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simpleDateFormat.format(cal.getTime());
	}
	
	

	
	public static int getSecondTimestamp(Date date){
		if (null == date) {
			return 0;
		}
		String timestamp = String.valueOf(date.getTime()/1000);
		return Integer.valueOf(timestamp);
	}
	public static int getSecondTimestamp(Date date,int day){
		if (null == date) {
			return 0;
		}
		Calendar ca = Calendar.getInstance();//获得一个Calendar类型的通用对象
	    ca.setTime(date);
	    ca.add(Calendar.DATE, day);
	    Date time = ca.getTime(); 
		String timestamp = String.valueOf(time.getTime()/1000);
		return Integer.valueOf(timestamp);
	}
	
	
	public static String stampToDate(long lt){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(lt);
		return simpleDateFormat.format(date);
	}
	public static String dateToStamp(String s) {
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long ts = date.getTime();
		res = String.valueOf(ts);
		return res;
	}
	public static long dateToStampS(String s) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			return 0;
		}
		long ts = date.getTime();
		return ts;
	}

	public static String oneWeekBefore(){
		Calendar c = Calendar.getInstance();
		//过去七天
		c.setTime(new Date());
		c.add(Calendar.DATE, - 7);
		Date d = c.getTime();
		String day = dateFormat.format(d);
		return day;
	}

	public static String oneMonthBefore(){
		Calendar c = Calendar.getInstance();
		//过去一月
		c.setTime(new Date());
		c.add(Calendar.MONTH, -1);
		Date d = c.getTime();
		String day = dateFormat.format(d);
		return day;
	}

	public static void main(String[] args) throws ParseException {
//		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//
//		Calendar c = Calendar.getInstance();
//
//		//过去七天
//		c.setTime(new Date());
//		c.add(Calendar.DATE, - 7);
//		Date d = c.getTime();
//		String day = format.format(d);
//		System.out.println("过去七天："+day);
//
//		//过去一月
//		c.setTime(new Date());
//		c.add(Calendar.MONTH, -1);
//		Date m = c.getTime();
//		String mon = format.format(m);
//		System.out.println("过去一个月："+mon);
		System.out.println(1691507785%4115);

		
	}

	public static String timeStamp2Date(String seconds,String format) {
		if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
			return "";
		}
		if(format == null || format.isEmpty()){
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(Long.valueOf(seconds+"000")));
	  }
	
	/**
	 * 转换下发的时间16进制数据
	 * @param s
	 * @return
	 */
	public static String dateToStampHex(Date date) {
		long ts = date.getTime() / 1000 + 60 * 60 * 8;
		return HexUtils.toHexL(ts+"", 8);
	}
	/**
	 * 转换下发的时间16进制数据
	 * @param s
	 * @return
	 */
	public static String dateToStampHex(String s) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long ts = date.getTime() / 1000 + 60 * 60 * 8;
		return HexUtils.toHexL(ts+"", 8);
	}
	/**
	 * 转换下发的时间16进制数据
	 * @param s
	 * @return
	 */
	public static String dateToStampHex(Date date,int day) {
		Calendar ca = Calendar.getInstance();//获得一个Calendar类型的通用对象
        ca.setTime(date);
        ca.add(Calendar.DATE, day);
        Date time = ca.getTime(); 
		long ts = time.getTime() / 1000 + 60 * 60 * 8;
		return HexUtils.toHexL(ts+"", 8);
	}
	/**
	 * 获取当前时间戳
	 * @return
	 */
	public static String getStamp() {
		return HexUtils.toHexL(DateUtil.getNowTime()+ 60 * 60 * 8 + "", 8);
	}
	/**
	 * 获取当前时间戳
	 * @return
	 */
	public static String getStamp(int min) {
		return HexUtils.toHexL(DateUtil.getNowTime()-min*60 + "", 8);
	}
	public static int getType(String startTime,String endTime) {
		long sTime , eTime;
		if(startTime.contains("-") && startTime.contains(":")) {
			 sTime = dateToStampS(startTime)/1000;
		}else {
			sTime =Long.parseLong(startTime);
		}
		if(endTime.contains("-") && endTime.contains(":")) {
			eTime = dateToStampS(endTime)/1000;
		} else {
			eTime =Long.parseLong(endTime);
		}
		long m = eTime -  sTime;
		long d = m /(24*60*60);
		if(m<=fiveMinDuration) {
			return 1;
		}else if(d<3) {
			return 2;
		}else if(d<=90) {
			return 3;
		}else {
			return 4;
		}
	}
	
	public static boolean contrastTime(long currentTim) {
		return currentTim-(getNowTime()+300)>0 ;
	}
	
	
	
}
